package com.youku.conductor
{
   import flash.display.Sprite;
   import com.youku.interfaces.IConductor;
   import com.youku.core.CoreFacade;
   import com.youku.interfaces.ICore;
   import com.youku.GlobalService;
   import com.youku.GlobalServiceKeys;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.FacadeManager;
   import com.youku.utils.YoukuPlayerError;
   import com.youku.PlayerConfig;
   import flash.display.MovieClip;
   import com.youku.events.ConductorEvent;
   
   public class Conductor extends Sprite implements IConductor
   {
      
      public function Conductor() {
         super();
         this.setCurrentState(ConductorState.INITIALIZE);
         this._newState = "";
         this._action = "";
         this._startPosition = 0;
         this._isDVDPlay = false;
      }
      
      protected var _coreFacade:CoreFacade;
      
      protected var _currentState:String;
      
      protected var _newState:String;
      
      protected var _action:String;
      
      private var _startPosition:Number;
      
      private var _isDVDPlay:Boolean = false;
      
      protected function get core() : ICore {
         return GlobalService.getService(GlobalServiceKeys.CORE) as ICore;
      }
      
      protected function get playerProxy() : IPlayerProxy {
         return GlobalService.getService(GlobalServiceKeys.PLAYER_PROXY) as IPlayerProxy;
      }
      
      protected function get facadeManager() : FacadeManager {
         return GlobalService.getService(GlobalServiceKeys.FACADE_MANAGER) as FacadeManager;
      }
      
      private function switchStateWithAction(param1:String) : void {
         switch(this._currentState)
         {
            case ConductorState.INITIALIZE:
               if(param1 != ConductorAction.LOAD_PLAYER)
               {
                  if(param1 != ConductorAction.LOAD_METADATA)
                  {
                     if(param1 == ConductorAction.MEET_ERROR)
                     {
                        this.switchState(ConductorState.ERROR);
                     }
                     else if(param1 == ConductorAction.LOAD_METADATA_DONE)
                     {
                        this.switchState(ConductorState.READY_TO_PLAY);
                     }
                     else if(param1 == ConductorAction.RESET)
                     {
                        this.switchState(ConductorState.INITIALIZE);
                     }
                     
                     
                  }
               }
               break;
            case ConductorState.READY_TO_PLAY:
               if(param1 == ConductorAction.PLAY)
               {
                  this.switchState(ConductorState.PRE_VIDEO);
               }
               else if(param1 == ConductorAction.RESET)
               {
                  this.switchState(ConductorState.INITIALIZE);
               }
               else if(param1 == ConductorAction.MEET_ERROR)
               {
                  this.switchState(ConductorState.ERROR);
               }
               
               
               break;
            case ConductorState.PRE_VIDEO:
               if(param1 == ConductorAction.PLAY_PRE_VIDEO_DONE)
               {
                  this.switchState(ConductorState.VIDEO);
               }
               else if(param1 == ConductorAction.RESET)
               {
                  this.switchState(ConductorState.INITIALIZE);
               }
               else if(param1 == ConductorAction.MEET_ERROR)
               {
                  this.switchState(ConductorState.ERROR);
               }
               
               
               break;
            case ConductorState.VIDEO:
               if(param1 == ConductorAction.PLAY_AD)
               {
                  this.switchState(ConductorState.AD);
               }
               else if(param1 == ConductorAction.PLAY_VIDEO_DONE)
               {
                  this.switchState(ConductorState.POST_VIDEO);
               }
               else if(param1 != ConductorAction.REPLAY)
               {
                  if(param1 == ConductorAction.RESET)
                  {
                     this.switchState(ConductorState.INITIALIZE);
                  }
                  else if(param1 == ConductorAction.MEET_ERROR)
                  {
                     this.switchState(ConductorState.ERROR);
                  }
                  
               }
               
               
               break;
            case ConductorState.AD:
               if(param1 == ConductorAction.PLAY_AD_DONE)
               {
                  this.switchState(ConductorState.VIDEO);
               }
               else if(param1 == ConductorAction.RESET)
               {
                  this.switchState(ConductorState.INITIALIZE);
               }
               else if(param1 == ConductorAction.MEET_ERROR)
               {
                  this.switchState(ConductorState.ERROR);
               }
               
               
               break;
            case ConductorState.POST_VIDEO:
               if(param1 == ConductorAction.PLAY_POST_VIDEO_DONE)
               {
                  this.switchState(ConductorState.VIDEO_END);
               }
               else if(param1 == ConductorAction.RESET)
               {
                  this.switchState(ConductorState.INITIALIZE);
               }
               else if(param1 == ConductorAction.MEET_ERROR)
               {
                  this.switchState(ConductorState.ERROR);
               }
               
               
               break;
            case ConductorState.VIDEO_END:
               if(param1 != ConductorAction.PLAY)
               {
                  if(param1 == ConductorAction.REPLAY)
                  {
                     this.switchState(ConductorState.VIDEO);
                  }
                  else if(param1 == ConductorAction.RESET)
                  {
                     this.switchState(ConductorState.INITIALIZE);
                  }
                  else if(param1 == ConductorAction.MEET_ERROR)
                  {
                     this.switchState(ConductorState.ERROR);
                  }
                  
                  
               }
               break;
            case ConductorState.ERROR:
               if(param1 == ConductorAction.RESET)
               {
                  this.switchState(ConductorState.INITIALIZE);
               }
               break;
         }
      }
      
      private function switchState(param1:String) : void {
         if(param1 == ConductorState.ERROR)
         {
            this.setCurrentState(param1);
            this.onStateToError();
            return;
         }
         if(param1 == ConductorState.INITIALIZE)
         {
            this.setCurrentState(param1);
            this.onStateToInitial();
            return;
         }
         switch(this._currentState)
         {
            case ConductorState.INITIALIZE:
               if(param1 == ConductorState.READY_TO_PLAY)
               {
                  this.setCurrentState(param1);
                  this.onStateToReadyToPlay();
                  break;
               }
               throw new YoukuPlayerError(YoukuPlayerError.STATE_CHANGE_ERROR + "From (" + this._currentState + ") To (" + param1 + ")");
            case ConductorState.READY_TO_PLAY:
               if(param1 == ConductorState.PRE_VIDEO)
               {
                  this.setCurrentState(param1);
                  this.onStateToPreVideo();
                  break;
               }
               throw new YoukuPlayerError(YoukuPlayerError.STATE_CHANGE_ERROR + "From (" + this._currentState + ") To (" + param1 + ")");
            case ConductorState.PRE_VIDEO:
               if(param1 == ConductorState.VIDEO)
               {
                  this.setCurrentState(param1);
                  this.onStateToVideo();
                  break;
               }
               throw new YoukuPlayerError(YoukuPlayerError.STATE_CHANGE_ERROR + "From (" + this._currentState + ") To (" + param1 + ")");
            case ConductorState.VIDEO:
               if(param1 == ConductorState.AD)
               {
                  this.setCurrentState(param1);
                  this.onStateToAd();
               }
               else if(param1 == ConductorState.POST_VIDEO)
               {
                  this.setCurrentState(param1);
                  this.onStateToPostVideo();
               }
               else
               {
                  throw new YoukuPlayerError(YoukuPlayerError.STATE_CHANGE_ERROR + "From (" + this._currentState + ") To (" + param1 + ")");
               }
               
               break;
            case ConductorState.AD:
               if(param1 == ConductorState.VIDEO)
               {
                  this.setCurrentState(param1);
                  this.onStateToVideoFromAD();
                  break;
               }
               throw new YoukuPlayerError(YoukuPlayerError.STATE_CHANGE_ERROR + "From (" + this._currentState + ") To (" + param1 + ")");
            case ConductorState.POST_VIDEO:
               if(param1 == ConductorState.VIDEO_END)
               {
                  this.setCurrentState(param1);
                  this.onStateToVideoEnd();
                  break;
               }
               throw new YoukuPlayerError(YoukuPlayerError.STATE_CHANGE_ERROR + "From (" + this._currentState + ") To (" + param1 + ")");
            case ConductorState.VIDEO_END:
               if(param1 == ConductorState.VIDEO)
               {
                  this.setCurrentState(param1);
                  this.onStateToVideoFromVideoEnd();
                  break;
               }
               throw new YoukuPlayerError(YoukuPlayerError.STATE_CHANGE_ERROR + "From (" + this._currentState + ") To (" + param1 + ")");
            case ConductorState.ERROR:
               break;
         }
      }
      
      private function onStateToVideoEnd() : void {
         this.facadeManager.showVideoEnd();
         this.facadeManager.onVideoEnd();
      }
      
      private function onStateToPostVideo() : void {
         this.facadeManager.playPostVideo();
      }
      
      private function onStateToAd() : void {
         this.facadeManager.playAD();
      }
      
      private function onStateToVideo() : void {
         this.facadeManager.playVideo();
      }
      
      private function onStateToVideoFromAD() : void {
         this.facadeManager.playADDone();
      }
      
      private function onStateToVideoFromVideoEnd() : void {
         this.facadeManager.replayVideoFromEnd(this._startPosition);
      }
      
      private function onStateToPreVideo() : void {
         this.facadeManager.playPreVideo();
      }
      
      private function onStateToReadyToPlay() : void {
         this.facadeManager.readyToPlay();
         if((PlayerConfig.rootData.autoPlay) || (PlayerConfig.dvdPlay))
         {
            this.switchStateWithAction(ConductorAction.PLAY);
         }
         else
         {
            this.waitUserPlay();
         }
      }
      
      private function onStateToError() : void {
         this.facadeManager.meetError();
      }
      
      private function onStateToInitial() : void {
         this.reset();
         this.facadeManager.reset();
      }
      
      private function reset() : void {
         this._startPosition = 0;
         if(!this._isDVDPlay)
         {
            PlayerConfig.dvdPlay = false;
            PlayerConfig.dvdVoiceVid = "";
         }
      }
      
      public function init(param1:MovieClip) : void {
         if(this._currentState == ConductorState.INITIALIZE)
         {
            this.setCurrentState(ConductorState.INITIALIZE);
            this._coreFacade = GlobalService.getService(GlobalServiceKeys.CORE_FACADE) as CoreFacade;
            this._coreFacade.init(param1,this.playerProxy);
         }
      }
      
      public function playerInitialized() : void {
         if(this._currentState == ConductorState.INITIALIZE)
         {
            this.switchStateWithAction(ConductorAction.LOAD_METADATA_DONE);
         }
      }
      
      public function postVideoDone() : void {
         this.switchStateWithAction(ConductorAction.PLAY_POST_VIDEO_DONE);
      }
      
      public function preVideoDone() : void {
         this.switchStateWithAction(ConductorAction.PLAY_PRE_VIDEO_DONE);
      }
      
      public function videoEnd() : void {
         this.switchStateWithAction(ConductorAction.PLAY_VIDEO_DONE);
      }
      
      public function adEnd() : void {
         this.switchStateWithAction(ConductorAction.PLAY_AD_DONE);
      }
      
      public function gotoPlayAd() : void {
         this.switchStateWithAction(ConductorAction.PLAY_AD);
      }
      
      private function waitUserPlay() : void {
         this.facadeManager.showReadyToPlay();
      }
      
      public function userPlay() : void {
         PlayerConfig.rootData.autoPlay = true;
         if(this._currentState == ConductorState.READY_TO_PLAY)
         {
            this.switchStateWithAction(ConductorAction.PLAY);
         }
         else if(this._currentState == ConductorState.VIDEO_END)
         {
            this.replay(0);
         }
         
      }
      
      public function get currentState() : String {
         return this._currentState;
      }
      
      protected function setCurrentState(param1:String) : void {
         this._currentState = param1;
         this.playerProxy.dispatchEvent(new ConductorEvent(ConductorEvent.STATE_CHANGED,this._currentState));
      }
      
      public function replay(param1:Number = 0) : void {
         this._startPosition = param1;
         this.switchStateWithAction(ConductorAction.REPLAY);
      }
      
      public function playNewVideo(param1:String, param2:Boolean = false) : void {
         this._isDVDPlay = param2;
         this.switchStateWithAction(ConductorAction.RESET);
         this.facadeManager.loadMetaData(param1);
      }
      
      public function stopVideo() : void {
         this.switchStateWithAction(ConductorAction.RESET);
      }
      
      public function meetFatalError() : void {
         this.switchStateWithAction(ConductorAction.MEET_ERROR);
      }
   }
}
